﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections;
using System.ComponentModel;

namespace Dominion.Core
{
    public class Supply 
    {

        public Supply()
        {
            Deck = new ObservableCollection<Card>();
            Discard = new ObservableCollection<Card>();
            Hand = new ObservableCollection<Card>();
            Played = new ObservableCollection<Card>();
            Buyed = new ObservableCollection<Card>();

        }

        #region Properties

        public ObservableCollection<Card> Deck { get; private set; }
        public ObservableCollection<Card> Discard { get; private set; }
        public ObservableCollection<Card> Hand { get; private set; }
        public ObservableCollection<Card> Played { get; private set; }
        public ObservableCollection<Card> Buyed { get; private set; }

        public static readonly int HandSize = 5;


        public int VictoryPoints
        {
            get
            {
                int sum = (from c in (Deck.Union(Discard).Union(Hand))
                           where c is IVictory
                           select c as IVictory)
                           .Sum(v => v.VictoryPoints);
                return sum;
            }
        }

        
        #endregion

        #region Public Methods

        public void DrawCard()
        {
            Card c = ObtainCardFromDeck();
            Hand.Push(c);
        }

        public void DrawCard(int nb)
        {
            nb.Foreach( c=> DrawCard());
        }

        public void DrawHand()
        {
            DrawCard(Supply.HandSize);
        }

          public void DiscardCard(Card card)
        {
            Discard.Push(card);
        }

        public void DiscardHand()
        {
            MoveAllCards(Hand, Discard);
        }


        public void DiscardDeck()
        {
            MoveAllCards(Deck, Discard);
        }


        public void DiscardPlayedCards()
        {
            MoveAllCards(Played, Discard);
        }

        public void DiscardBuyedCards()
        {
            MoveAllCards(Buyed, Discard);
        }


        #endregion

        #region Private Methods


        private void RegenerateDeck()
        {
            MoveAllCards(Discard, Deck);
            Deck.Shuffle();
        }

        private Card ObtainCardFromDeck()
        {
            Card c;
            if (!Deck.Any())
            {
                RegenerateDeck();
            }
            c = Deck.Pop();
            return c;
        }

        private void MoveAllCards(IList<Card> source, IList<Card> destination)
        {
            int nbCarte = source.Count;
            for (int i = 0; i < nbCarte; i++)
            {
                Card c = source.Pop();
                destination.Push(c);
            }
        }

    }

    #endregion

    
}
